﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    public class MarketService
    {
        public MarketService() { }

        public void AddMarket(Market market)
        {
            using (var ctx = new SystemContext())
            {
                if (market == null) { return; }
                ctx.Markets.Add(market);
                ctx.SaveChanges();
            }
        }
        public void DeleteMarket(Market market)
        {
            using (var ctx = new SystemContext())
            {
                var market1 = ctx.Markets
                    .SingleOrDefault(s => s.MarketID == market.MarketID);

                if (market1 == null) { return; }
                ctx.Markets.Remove(market1);
                ctx.SaveChanges();
            }
        }
        public void ModifyMarket(Market market)
        {
            DeleteMarket(market);
            AddMarket(market);
        }
        public List<Market> GetAllMarket()
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Markets
                    .ToList();
            }
        }
        public List<Market> GetMarketByMarketID(string marketid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Markets
                    .Where(s => s.MarketID == marketid)
                    .ToList();
            }
        }
        public List<Market> GetMarketByUserID(string userid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Markets
                    .Where(s => s.UserID == userid)
                    .ToList();
            }
        }
    }
}
